class code2 {
    //进阶做法:借助hashmap+双指针
    public int findClosest(String[] words, String word1, String word2) {
        int ret = Integer.MAX_VALUE;
        Map<String,List<Integer>> map = new HashMap<>();
        //遍历words 把对应 下标i 放入
        for(int i=0;i<words.length;i++) {
            map.putIfAbsent(words[i],new ArrayList<>());
            map.get(words[i]).add(i);
        }
        //根据hash取最小值
        List<Integer> list1 = map.get(word1);
        List<Integer> list2 = map.get(word2);
        int i = 0;
        int j = 0;
        while(i<list1.size() && j<list2.size()) {
            //去除对应下标
            int a = list1.get(i);
            int b = list2.get(j);

            ret = Math.min(ret,Math.abs(a-b));

            if(a < b) {
                i++;
            } else {
                j++;
            }
        }
        return ret;
    }
}